<?php
/*
 * 订单列表
 */
namespace Mobile\Controller;

use Mobile\Model\orderModel;

class OrderlistsController extends BaseController {

    public $orderModel;

    public function __construct() {
        parent::__construct();
        $this->orderModel = new orderModel();
    }
    
    
    //订单查询条件
    protected function _get_order_where(){
        $where = "1 AND uid=".$this->uid;
        //0待支付,1已付款,,2完成,3.待退款 4已退款,5发货,0未完成,6订单关闭,
        $status = get('status');
        if($status == 9){
            $where .= " AND checked=0";
        }else if($status == 1){
            $where .= " AND checked=1";
        }else if($status == 5){
            $where .= " AND checked=5";
        }else if($status == 3){
            $where .= " AND checked IN(3,4)";
        }else if($status == 2){
            $where .= " AND checked=2";
        }else if($status == 6){
            $where .= " AND checked=6";
        }
        return $where;
    }
    

    /*
     * 订单统计
     * //0待支付,1已付款,,2完成,3.待退款 4已退款,5发货,0未完成,6订单关闭,
     */
    public function countOrder($uid=0){
        if($uid){
            $where = "uid=".$uid;
        }else{
            $where = "1 ";
        }
        
        $count[0] = $this->orderModel->getOrderCount($where." AND checked=0");//待支付
        $count[1] = $this->orderModel->getOrderCount($where." AND checked=1");//待支付
        $count[2] = $this->orderModel->getOrderCount($where." AND checked=2");//待支付
        $count[3] = $this->orderModel->getOrderCount($where." AND checked=3");//待支付
        $count[4] = $this->orderModel->getOrderCount($where." AND checked=4");//待支付
        $count[5] = $this->orderModel->getOrderCount($where." AND checked=5");//待支付
        $count[6] = $this->orderModel->getOrderCount($where." AND checked=6");//待支付
        
        $count[10] = $this->orderModel->getOrderCount($where);//全部订单
        $count[11] = $this->orderModel->getOrderCount($where." AND checked IN(3,4)");//退货和已退
        
        return $count;
    }
    
    
    //获取订单支付状态文字
    //TODO 货到付款
    //0待支付,1已付款,,2完成,3.待退款 4已退款,5发货,0未完成,6订单关闭,
    protected function _order_status($stutus=0){
        $status_arr = array(
            "待付款","待发货","已完成","待退款","已退款","待收货","已关闭"
        );
        return $status_arr[$stutus];
    }

    /*
     * 订单列表
     * 
     */
    public function index(){
        
        $seo_data = array(
            "title" => $this->lang['order_list'],
            "description" => "",
            "keywords" => "",
        );
        $this->setSeo($seo_data);
        
        $page = get('p',1);
        $where = $this->_get_order_where();
        
        $order_list = $this->orderModel->getOrderList($where, $page);
        foreach($order_list as $k=>$v){
            $order_list[$k]['status'] = $this->_order_status($v['checked']);
            $order_list[$k]['goods'] = unserialize($order_list[$k]['goods']);//反序列化
        }

        $share_data = array(
            "title" => $this->lang['order_list'],
            "img" => $this->memberinfo['headimgurl'],
            "url" => get_domain()."/mobile/orderlists?from=sharelink&suid=".$this->uid,
        );
        $this->setShare($share_data);
        
        $this->assign('order_list',$order_list);
        $this->display();
        
    }

}